import type { App } from 'vue';
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import Dashboard from '@/views/Dashboard/index.vue';
import AppLayout from '@/components/Layout/index.vue';
import routerGuards from './routerGuards';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: AppLayout,
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
          title: '工作台',
        },
      },
      {
        path: 'cooperation-goods',
        component: () => import('@/views/CooperationGoods/index.vue'),
        meta: {
          title: '合作商品列表',
        },
        children: [
          {
            path: '',
            name: 'CooperationGoods',
            component: () =>
              import('@/views/CooperationGoods/CooperationGoods.vue'),
          },
          {
            path: ':id',
            name: 'PreviewProduct',
            component: () =>
              import('@/views/CooperationGoods/PreviewProduct/index.vue'),
            meta: {
              title: '预览商品',
            },
          },
          {
            path: 'material',
            name: 'CooperationGoodsMaterial',
            component: () =>
              import('@/views/CooperationGoods/Material/index.vue'),
            meta: {
              title: '招生物料',
            },
          },
          {
            path: '/check-contract',
            name: 'CheckContract',
            component: () =>
              import('@/views/CooperationGoods/CheckContract/index.vue'),
            meta: {
              title: '查看合同',
            },
          },
        ],
      },
      {
        path: 'sign-up',
        component: () => import('@/views/SignUp/index.vue'),
        meta: {
          title: '商品报名',
        },
        children: [
          {
            path: '',
            name: 'SignUp',
            component: () => import('@/views/SignUp/SignUp.vue'),
          },
          {
            path: 'form/:id?',
            name: 'SignUpForm',
            component: () => import('@/views/SignUp/FormPage/index.vue'),
            meta: {
              title: '新增报名',
            },
            beforeEnter(to) {
              if (to.params?.id) {
                to.matched[to.matched.length - 1].meta.title = '编辑报名信息';
                to.meta.title = '编辑报名信息';
              }
            },
          },
        ],
      },
      {
        path: 'study-file',
        component: () => import('@/views/StudyFile/index.vue'),
        meta: {
          title: '学习档案',
        },
        children: [
          {
            path: '',
            name: 'StudyFile',
            component: () => import('@/views/StudyFile/StudyFile.vue'),
          },
          {
            path: 'check',
            name: 'StudyFileCheck',
            component: () => import('@/views/StudyFile/CheckPage/index.vue'),
            meta: {
              title: '学员学习情况',
            },
          },
        ],
      },
      {
        path: '/trainee',
        name: 'Trainee',
        component: () => import('@/views/Trainee/index.vue'),
        meta: {
          title: '用户',
        },
      },
      {
        path: '/info-manage',
        component: () => import('@/views/InfoManage/index.vue'),
        meta: {
          title: '信息维护',
        },
        children: [
          {
            path: '',
            name: 'InfoManage',
            component: () => import('@/views/InfoManage/InfoManage.vue'),
            meta: {
              title: '渠道信息',
            },
          },
          {
            path: '/change-pwd',
            name: 'ChangePwd',
            component: () => import('@/views/InfoManage/ChangePwd.vue'),
            meta: {
              title: '修改密码',
            },
          },
        ],
      },
      {
        path: '/not-found',
        name: 'NotFound',
        component: () => import('@/views/NotFound/index.vue'),
        meta: {
          title: '未找到此页面',
        },
      },
    ],
  },
  {
    path: '/:catchAll(.*)',
    redirect: '/not-found',
  },
  {
    path: '/login',
    name: 'Login',
    component: () =>
      import(/* webpackChunkName: "login" */ '@/views/Login/index.vue'),
    meta: {
      title: '登录',
    },
  },
];

export const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export function setupRouter(app: App) {
  app.use(router);
  // 路由导航函数
  routerGuards(router);
}
